<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="关联的订单ID" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入关联的订单ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退款金额" prop="amount">
        <el-input
          v-model="queryParams.amount"
          placeholder="请输入退款金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="getPendingList">待审核</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="getApprovedList">已同意</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="getRejectedList">拒绝</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['shop:after_sales_requests:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['shop:after_sales_requests:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['shop:after_sales_requests:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['shop:after_sales_requests:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="after_sales_requestsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="售后申请的ID" align="center" prop="id" />
      <el-table-column label="关联的订单ID" align="center" prop="orderId" />
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="售后类型" align="center" prop="requestType" >
        <template v-slot:default="scope">
          <span v-if="scope.row.requestType === 'refund'">退款</span>
          <span v-else-if="scope.row.requestType === 'exchange'">换货</span>
          <span v-else>未知</span>
        </template>
      </el-table-column>
      <el-table-column label="申请原因" align="center" prop="requestReason" />
      <el-table-column label="申请状态" align="center" prop="requestStatus" >
        <template v-slot:default="scope">
          <el-tag v-if="scope.row.requestStatus === 'pending'">待审核</el-tag>
          <el-tag type="success" v-else-if="scope.row.requestStatus === 'approved'">已同意</el-tag>
          <el-tag type="danger" v-else-if="scope.row.requestStatus === 'rejected'">拒绝</el-tag>
          <el-tag type="danger" v-else>未知</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="退款金额" align="center" prop="amount" />
      <el-table-column label="操作备注" align="center" prop="comment" />
      <el-table-column label="操作" align="center" min-width="200" width="" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['shop:after_sales_requests:edit']"
          >编辑</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['shop:after_sales_requests:remove']"-->
<!--          >删除</el-button>-->


          <!--          v-hasPermi="['shop:after_sales_requests:remove']"-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-check"
            @click="handleAgree(scope.row)"
          >同意</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-close"
            @click="handleRefuse(scope.row)"
          >拒绝</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改售后详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="关联的订单ID" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入关联的订单ID" readonly/>
        </el-form-item>
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户ID" readonly/>
        </el-form-item>
        <el-form-item label="申请原因" prop="requestReason">
          <el-input v-model="form.requestReason" type="textarea" placeholder="请输入内容" readonly/>
        </el-form-item>
        <el-form-item label="退款金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入退款金额" />
        </el-form-item>
        <el-form-item label="操作备注" prop="comment">
          <el-input v-model="form.comment" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


<!--    换货-->
<!--    <el-dialog :visible.sync="isDialog" @open="onOpen" @close="onClose" title="换货">-->
    <el-dialog  @open="onOpen" @close="onClose" title="换货">
      <el-form ref="elForm" :model="formData" :rules="rulesLogistics" size="medium" label-width="100px">
        <el-form-item label="快递单号" prop="field101">
          <el-input v-model="formData.field101" placeholder="请输入快递单号" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAfter_sales_requests,
  getAfter_sales_requests,
  delAfter_sales_requests,
  addAfter_sales_requests,
  updateAfter_sales_requests,
  agree_requests, refuse_requests
} from '@/api/shop/after_sales_requests'

export default {
  name: "After_sales_requests",
  inheritAttrs: false,
  components: {},
  props: [],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 售后详情表格数据
      after_sales_requestsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderId: null,
        userId: null,
        requestType: null,
        requestReason: null,
        requestStatus: null,
        amount: null,
        comment: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderId: [
          { required: true, message: "关联的订单ID不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "用户ID不能为空", trigger: "blur" }
        ],
        requestType: [
          { required: true, message: "售后类型(退款/换货)不能为空", trigger: "change" }
        ],
      },
      formData: {
        field101: undefined,
      },
      rulesLogistics: {
        field101: [{
          required: true,
          message: '请输入快递单号',
          trigger: 'blur'
        }],
      },
      isDialog:false,
      exchangeData:{}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询售后详情列表 */
    getList() {
      this.loading = true;
      listAfter_sales_requests(this.queryParams).then(response => {
        this.after_sales_requestsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getPendingList(){
      this.queryParams.pageNum = 1;
      this.queryParams.requestStatus = "pending";
      this.handleQuery();
    },
    getApprovedList(){
      this.queryParams.pageNum = 1;
      this.queryParams.requestStatus = "approved";
      this.handleQuery();
    },
    getRejectedList(){
      this.queryParams.pageNum = 1;
      this.queryParams.requestStatus = "rejected";
      this.handleQuery();
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderId: null,
        userId: null,
        requestType: null,
        requestReason: null,
        requestStatus: null,
        amount: null,
        comment: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加售后详情";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAfter_sales_requests(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改售后详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAfter_sales_requests(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAfter_sales_requests(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除售后详情编号为"' + ids + '"的数据项？').then(function() {
        return delAfter_sales_requests(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    handleAgree(row){
      this.exchangeData = row;
      this.exchangeData.reason = row.requestReason;
      console.log(row)
      if (row.requestType == 'refund'){
        //退款
        agree_requests(this.exchangeData).then(response => {
          // this.close()
          this.$message({
            message: '执行中',
            type: 'success',
            duration: 3000 // 默认 3000ms，可不写
          });
        })
      }else if(row.requestType == 'exchange'){
        //换货
        this.isDialog = true;
        agree_requests(this.exchangeData).then(response => {
          // this.close()
          this.$message({
            message: '执行中',
            type: 'success',
            duration: 3000 // 默认 3000ms，可不写
          });
        })
      }else {

      }
    },
    handleRefuse(data){
      refuse_requests(data.id).then(response => {
        // console.log(response.code)
        if (response.code == 200) {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 3000 // 默认 3000ms，可不写
          });
        }
      })
    },
    onOpen() {},
    onClose() {
      this.isDialog = false;
      this.$refs['elForm'].resetFields()
      this.$emit('update:visible', false)
    },
    close() {
      this.isDialog = false;
      this.$refs['elForm'].resetFields()
      this.$emit('update:visible', false)
    },
    handleConfirm() {
      this.exchangeData.logisticsId = this.formData.field101;
      agree_requests(this.exchangeData).then(response => {
          this.close()
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('shop/after_sales_requests/export', {
        ...this.queryParams
      }, `after_sales_requests_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
